package com.zzxx.wechat.demo;

import cn.hutool.http.HttpUtil;
import com.zzxx.wechat.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
@Slf4j
public class ProxyAuthenticatedRequest {

    public static void main(String[] args) throws Exception {
        String url = "https://arcteryx.com/us/en/shop/X000009141";
      //  String url = "https://www.baidu.com";
        String proxyHost = "brd.superproxy.io";
        int proxyPort = 33335;
        String proxyUser = "customer-zhangxin_iUsnU-cc-US";
        String proxyPassword = "awsAWS123+++";

        // 设置代理及认证信息
        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(
                new HttpHost(proxyHost, proxyPort));

        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(
                new AuthScope(proxyHost, proxyPort),
                new UsernamePasswordCredentials(proxyUser, proxyPassword));
        // 信任所有证书
        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (chain, authType) -> true).build();
        SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext);
        try (CloseableHttpClient client = HttpClients.custom()
                .setRoutePlanner(routePlanner)
              .setDefaultCredentialsProvider(credentialsProvider)
                .setSSLSocketFactory(sslFactory)
                .build()) {
         //  SslUtils.ignoreSsl();
            HttpGet request = new HttpGet(url);
            request.setHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");

            try (CloseableHttpResponse response = client.execute(request)) {
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Response Status Code: " + statusCode);

                if (statusCode == 200) {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        String htmlStr = EntityUtils.toString(entity);
                        System.out.println("HTML Content Length: " + htmlStr.length());

                        // 使用 Jsoup 解析 HTML
                        Document document = Jsoup.parse(htmlStr);
                        Elements element = document.select("#__NEXT_DATA__");
                        String html = element.html();
                        if (StringUtils.isEmpty(html)) {
                            log.error("解析URL返回为空,链接:{}", url);
                            throw new BusinessException("解析URL返回为空,请确认输入链接正确");
                        }
                        log.info("解析URL返回JSON:{}", html);
                    }
                } else {
                    System.err.println("Failed to fetch the page. Status code: " + statusCode);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
